package com.dai.controller;

import com.dai.pojo.Books;
import com.dai.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;


    //查询全部书籍
    @RequestMapping("/allBook")
    public String list(Model model){

        List<Books> list= bookService.queryAllBooks();
        model.addAttribute("list",list);
        return "allBook";
    }

    //跳转增加页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }

    //添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }

    //跳转到修改界面
    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("QBook",books);
        System.out.println(books);
        return "Update";
    }

    //修改书籍
    @RequestMapping("/update")
    public String UpdatePaper(Books books){
        System.out.println("updateBook------"+books);
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }

    //删除书籍
    @RequestMapping("/delete/{bookId}")
    public String deletePaper(@PathVariable("bookId") int id){
        System.out.println("delete--------");
        bookService.deleteBook(id);

        return  "redirect:/book/allBook";
    }

    //查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName,Model model){
        System.out.println("查询----------------");
        Books books = bookService.queryBookName(queryBookName);
        List<Books> list=new ArrayList<>();
        list.add(books);
        System.err.println(books);
        if(books==null){
            list= bookService.queryAllBooks();
            model.addAttribute("error","未查到");
        }
            model.addAttribute("list",list);

        return "allBook";
    }

}
